﻿(function () { 
    $goodsBox = $('.goodsget'), $copyright = $('.copyright_box');
    var funList = {
        setting: function () {
            showTkl($goodsBox);
        },
        init: function () {
            showTkl($goodsBox);
        }
    }
    funList.init();
})();





//关闭淘口令弹窗
function closeMask() {
    $('#mask_close').on('click', function () {
        $('#mask').hide();
    });
}

//弹出淘口令
//$list为列表的容器 other为额外操作如获取淘口令getKtl isprice为是否显示券后价，详情没有
function showTkl($list, other, isprice) {
   
    var $copyBtn = $('#copy_kl'),
        $mask = $('#mask'),
        $copySuccess = $('#copy_success'),
        $title = $('#mask_title');
    //点击复制
    $copyBtn.on('click', function () {
        $copySuccess.show();
        setTimeout(function () {
            $copySuccess.hide();
        }, 1500);
    });
    //关闭遮罩
    closeMask();
    $list.on('click', function () {
      
        var $that = $(this),
            waiting = false;
        //点击下载块不执行
        if ($that.hasClass('app_url')) {
            return false;
        }
        //点击获取淘口令再弹窗
        if (other == 'getKtl') {
            if (waiting) {
                return false;
            }
            waiting = true;
            $.ajax({
                url: "/ajax/GetKouling",
                data: 'url=' + encodeURIComponent($that.data('url')) + '&img=' + $that.data('img') + '&title=' + $that.data('title'),
                type: "GET",
                success: function (data) {
                    $that.data('tkl', data.kouling);
                    waiting = false;
                    if (isWx()) {
                        var tkl = $that.data('tkl'),
                            value = '';
                        //没有淘口令
                        if (tkl == '' || typeof tkl === 'undefined') {
                            window.location.href = $that.data('url');
                            return false;
                        }
                        if (isprice == 'noPrice') {
                            value = '【' + $that.data('title') + '】复制这条信息，打开手机淘宝' + tkl;
                        } else {
                            value = '【' + $that.data('title') + '，券后' + $that.data('price') + '元包邮】复制这条信息，打开手机淘宝' + tkl;
                        }
                        //配置数据
                        $title.html(value);
                        $copyBtn.data('clipboard-text', value);
                        var clipboard = new Clipboard('#copy_kl');
                        //不支持复制
                        clipboard.on('error', function (e) {
                            $copyBtn.remove();
                            $('.mask_top').show();
                        });
                        $mask.show();
                    } else {
                        //不是微信直接跳转
                        window.location.href = $that.data('url');
                    }
                },
                error: function () {
                    waiting = false;
                    promptPop({ content: '系统繁忙,请重试' });
                }
            });
            return false;
        }
        //已经有淘口令再弹窗
        //if (isWx()) {
            var tkl = $that.data('tkl');
            //没有淘口令
            if (tkl == '' || typeof tkl === 'undefined') {
                window.location.href = $(this).data('url');
                return false;
            }
            var value = '【' + $that.data('title') + '，券后' + $that.data('price') + '元包邮】复制这条信息，打开手机淘宝' + tkl;
            //配置数据
            $title.html(value);
            $copyBtn.data('clipboard-text', value);
            var clipboard = new Clipboard('#copy_kl');
            //不支持复制
            clipboard.on('error', function (e) {
                $copyBtn.remove();
                $('.mask_top').show();
            });
            $mask.show();
        //} else {
            //不是微信直接跳转
           // window.location.href = $(this).data('url');
        //}
    });
}
